package org.goldfish.client_demo.client_demo.controller;

import org.goldfish.client_demo.client_demo.service.Query;
import org.goldfish.client_demo.client_demo.service.RegService;
import org.goldfish.distributed_framework.client.RpcClient;
import org.goldfish.distributed_framework.client.RpcResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;
import java.util.Objects;
import java.util.Properties;

import static org.goldfish.distributed_framework.client.RpcResponse.success;

// 控制器类，处理与招聘相关API请求
@RestController
@RequestMapping("/api")
public class RecruitmentController {
    @Autowired
    private Query query;
    @Autowired
    private RegService regService;

    private boolean useRemote;

    public RecruitmentController() {
        Properties properties = new Properties();
        String properties_filename = "client.properties";
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(properties_filename);
        Objects.requireNonNull(inputStream, "Properties file not found: " + properties_filename);
        try {
            properties.load(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String useRemoteRaw=properties.getProperty("client.useRemote");
        useRemote = Boolean.parseBoolean(useRemoteRaw);
        System.out.println("useRemote:" + useRemote);
    }

    /**
     * 注册职位信息
     *
     * @param jobName  职位名称
     * @param jobCount 职位数量
     * @return 职位注册结果
     */
    @GetMapping("/register")
    public ResponseEntity<Object> registerJob(
            @RequestParam(value = "jobName")
            String jobName,
            @RequestParam(value = "jobCount")
            long jobCount
    ) {
        if (useRemote)
            // 调用RpcClient的call方法向"registry"服务发送职位名称和数量，处理职位注册请求
            return ResponseEntity.ok(RpcClient.call("registry", "", jobName, jobCount));
        RpcResponse response = new RpcResponse();
        try {
            response.setResult(regService.reg(jobName, jobCount));
            response.setSuccess(true);
        } catch (Exception e) {
            response.setError(e.getMessage());
            response.setSuccess(false);
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 查询职位信息
     *
     * @param jobName 职位名称，可选参数
     * @return 职位查询结果
     */
    @GetMapping("/query")
    public ResponseEntity<Object> queryJob(
            @RequestParam(value = "jobName", required = false)
            String jobName
    ) {
        Object body;
        if(!useRemote){
            if(jobName == null|| jobName.isEmpty()){
                body = query.queryAll();
            }else{
                body = query.query(jobName);
            }
            return ResponseEntity.ok((body));
        }
        // 如果职位名称为空，则执行全局查询
        if (jobName == null || jobName.isEmpty()) {
            System.out.println("全局查询");
            // 调用RpcClient的call方法向"query"服务发送空参数，处理全局职位查询请求
            body = RpcClient.call("query", "");
        } else {
            // 如果职位名称不为空，打印职位名称并执行特定查询
            System.out.println("jobName:" + jobName);
            // 调用RpcClient的call方法向"query"服务发送特定职位名称，处理特定职位查询请求
            body = RpcClient.call("query", "", jobName);
        }
        return ResponseEntity.ok(body);
    }

}
